<?php
    require_once('../Database/Database.php');
    if(isset($_POST['series_id']) && isset($_POST['driver_name']) && isset($_POST['joomla_id']))
    {
        $id         = $_POST['series_id'];
        $name       = $_POST['driver_name'];
        $jid        = $_POST['joomla_id'];
        $number     = $_POST['select_number'];
        $team_id    = $_POST['select_team'];
        $vehicle_id = $_POST['select_vehicle'];
        $steam      = false;
        if(isset($_POST['steam_name']))
        {
            $steam = $_POST['steam_name'];
        }

        $db = new Database();
        $db->Connect();

        $shortcut = $db->SelectColumns('Series', 'Shortcut', 'ID_Series = ' . $id);
        if($shortcut)
        {
            $shortcut = $shortcut[0]->Shortcut;

            if(!$db->Select($shortcut . '_drivers', 'JID = ' . $jid))
            {
                $columnArray = array('JID'     , 'Name', 'Number'      , 'Team_ID'     , 'Vehicle_ID'     , 'Active');
                $valueArray  = array((int)$jid , $name , (int)$number  , (int)$team_id , (int)$vehicle_id , 0);

                if($db->Insert($shortcut . '_drivers', $columnArray, $valueArray))
                {
                    $driver = $db->Select($shortcut . '_drivers', 'JID = ' . $jid);

                    if($driver)
                    {
                        $input_string = 'Drivers';
                        $value_string = '\'' . $driver[0]->ID_Driver . '\'';

                        $db->Update($shortcut . '_teams', $input_string, $value_string);
                    }
                    echo 'Successfully signed up.';
                }
                else
                {
                    echo 'Signup error.';
                }
                $db->Disconnect();
            }
            else
            {
                echo 'Bereits angemeldet.';
            }
        }
        else
        {
            echo 'Ung&uuml;ltige Serien ID.';
        }
    }
    else
    {
        echo 'ERROR';
    }
?>
